package net.cellkyborg.RSInstances;

import net.cellkyborg.server.stream;

public class RSNPC extends RSAttackable {

	public RSNPC() {
		super();
		changeNPCIDUpdateRequired = false;
	}

	public int npcID;
	public int slot;
	
	public boolean changeNPCIDUpdateRequired; 
	
	public void process() {
		try {
			resetNPCUpdateVars();
			processWalking();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	private void resetNPCUpdateVars() {
		updateRequired = false;
		animationUpdateRequired = false;
		faceToNPCUpdateRequired = false;
		changeNPCIDUpdateRequired = false;
		hitUpdateRequired = false;
		hit2UpdateRequired = false;
		chatTextUpdateRequired = false;
		faceToUpdateRequired = false;
		tempUpdateRequired = false;
	}
	
	public void updateNPCMovement(stream str) {
		if (dir1 == -1) {
			if (updateRequired) {
				str.writeBits(1, 1);
				str.writeBits(2, 0);
			} else {
				str.writeBits(1, 0);
			}
		} else {
			str.writeBits(1, 1);

			if (dir2 == -1) {
				str.writeBits(2, 1);
				str.writeBits(3, dir1);
				if (updateRequired) {
					str.writeBits(1, 1);
				} else {
					str.writeBits(1, 0);
				}
			} else {
				str.writeBits(2, 2);
				str.writeBits(3, dir1);
				str.writeBits(3, dir2);
				if (updateRequired) {
					str.writeBits(1, 1);
				} else {
					str.writeBits(1, 0);
				}
			}
		}
	}
	
	public boolean tempUpdateRequired = false;
	public int tempUpdateID = 0;
	public int tempUpdateID2 = 0;
	
	public void appendNPCUpdate(stream updateStream) {
		if (!updateRequired)
			return;
		/*
		 * 0x10 Animation update (done)
		 * 0x4 face to NPC (done)
		 * 0x2 replace NPC (change NPCID) (done)
		 * 0x40 Something with facing
		 * 0x80 Hit update (half done)
		 * 0x20 Another hit update?? (half done)
		 * 0x1 Chat text (done)
		 * 0x8 Graphics
		 */

		int updateMask = 0;
		
		if (animationUpdateRequired)
			updateMask |= 0x10;
		if (faceToNPCUpdateRequired)
			updateMask |= 0x4;
		if (changeNPCIDUpdateRequired)
			updateMask |= 0x2;
		if (tempUpdateRequired)
			updateMask |= 0x40;
		if (hitUpdateRequired)
			updateMask |= 0x80;
		if (hit2UpdateRequired)
			updateMask |= 0x20;
		if (chatTextUpdateRequired)
			updateMask |= 0x1;
		updateStream.writeByte(updateMask);
		if (animationUpdateRequired)
			appendAnimationUpdate(updateStream);
		if (faceToNPCUpdateRequired)
			appendFaceToNPCUpdate(updateStream);
		if (changeNPCIDUpdateRequired)
			appendChangeNPCIDUpdate(updateStream);
		if (tempUpdateRequired)
			appendTempUpdate(updateStream);
		if (hitUpdateRequired)
			appendHitUpdate(updateStream);
		if (hit2UpdateRequired)
			appendHit2Update(updateStream);
		if (chatTextUpdateRequired)
			appendChatTextUpdate(updateStream);
	}
	
	private void appendAnimationUpdate(stream updateStream) {
		updateStream.writeWordBigEndianA(animationUpdateID);
		updateStream.writeByteC(animationUpdateDelay);//wait time
	}

	private void appendFaceToNPCUpdate(stream updateStream) {
		updateStream.writeWord(faceToNPCUpdateID);
	}
	
	private void appendChangeNPCIDUpdate(stream updateStream) {
		updateStream.writeWordBigEndianA(npcID);
	}
	
	private void appendHitUpdate(stream updateStream) {
		updateStream.writeByte(1);//damage
		updateStream.writeByteA(2);//color
		updateStream.writeByte(3);//current hp
		updateStream.writeByteS(4);//max hp
	}

	private void appendHit2Update(stream updateStream) {
		updateStream.writeByte(1);//damage
		updateStream.writeByteA(2);//color
		updateStream.writeByte(3);//current hp
		updateStream.writeByteS(4);//max hp
	}

	private void appendChatTextUpdate(stream updateStream) {
		updateStream.writeString(chatText);
	}
	
	
	private void appendTempUpdate(stream updateStream) {
		updateStream.writeWordBigEndianA(tempUpdateID);
		updateStream.writeWordBigEndianA(tempUpdateID2);
	}
	
	public void handleWalkAction() {
		resetWalkAction();
	}
}
